﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using NUnit.Framework;
using SimpleFS.Core;
using SimpleFS.Services.Transactions;
using SimpleFS.Services.Transactions.Implementations;

namespace SimpleFS.Specs.Services.Transactions
{
    /// <summary>
    /// Atomic Operations for a Directory:
    /// 1. Delete
    /// 2. Rename
    /// 3. Create
    /// </summary>
    public class DirectoryTest : AbstractFileSystemObjectTest
    {
        protected IResourceManager resourceManager = null;

        public DirectoryTest()
        {
            this.resourceManager = new SimpleResourceManager();
        }

        protected override SimpleFS.Core.IFileSystemObject SourceObject
        {
            get
            {
                return
                    new SimpleFS.Services.Transactions.Directory(
                  new SimpleFS.Storage.Windows.Directory(
                    "../../Resources/Source/"), this.resourceManager);
            }
        }

        protected override SimpleFS.Core.IFileSystemObject TargetObject
        {
            get
            {
                return
                    new SimpleFS.Services.Transactions.Directory(
                  new SimpleFS.Storage.Windows.Directory(
                    "../../Resources/Target/"), this.resourceManager);
            }
        }

        protected override void EnsureObjectExists(SimpleFS.Core.IFileSystemObject fileSystemObject)
        {
            if (!System.IO.Directory.Exists(fileSystemObject.Name))
            {
                System.IO.Directory.CreateDirectory(fileSystemObject.Name);
            }
        }

        protected override void EnsureObjectIsDeleted(SimpleFS.Core.IFileSystemObject fileSystemObject)
        {
            if (System.IO.Directory.Exists(fileSystemObject.Name))
            {
                System.IO.Directory.Delete(fileSystemObject.Name);
            }
        }

        [Test]
        public void TestCreate()
        {
            IDirectory sourceDirectory = this.SourceObject as IDirectory;
            IDirectory targetDirectory = this.TargetObject as IDirectory;

            using (TransactionScope scope = new TransactionScope())
            {
                targetDirectory.Create();
                scope.Complete();
            }
            Assert.IsTrue(targetDirectory.Exists());

            this.EnsureObjectIsDeleted(targetDirectory);

            using (TransactionScope scope = new TransactionScope())
            {
                targetDirectory.Create();
            }
            Assert.IsFalse(targetDirectory.Exists());
        }

        [Test]
        public override void TestCopy()
        {
            base.TestCopy();
        }

        [Test]
        public override void TestDelete()
        {
            base.TestDelete();
        }

        [Test]
        public override void TestMove()
        {
            base.TestMove();
        }

        [Test]
        public override void TestRename()
        {
            base.TestRename();
        }
    }
}
